#!/bin/bash
# File description manager
# Date: 20100731
# Author: Carlos Jose Barroso <cjbarroso@gmail.com>
# Description:
#   
# A simple file metadata and descriptions manager for bash

DESCFILE=".fdesc"

if [ -z $2 ]; then
  echo "File missing!"
  exit
fi

if [ ! -e $2 -a $2 != "+" ]; then
  echo "File does not exists"
  exit
fi

# operation
OP=$1
shift;
# filename
NARCH=`basename $1`
# dirname
DIR=`dirname $1`
shift; # 
# description file near the destination file
DESCFILE=$DIR/$DESCFILE

case "$OP" in
  new|add|n|a)
    if [ -z $1 ]; then
      read -p "Description for $NARCH ? " DESC
    else
      DESC=$*
    fi
    echo "$NARCH:$DESC" >> $DESCFILE
    ;;
  ver|v)
    if [ $NARCH == "+" ]; then
      cat $DESCFILE
      exit
    fi
    grep "^$NARCH:" $DESCFILE | cut -d":" -f2 
    ;;
  del|d)
    sed "/^$NARCH:/d" $DESCFILE
    ;;
  *)
    echo "Usage: "
    echo "`basename $0` a <file> [desc]: Add a descriptory line for <file>"
    echo "`basename $0` v <file>: Show descriptions for <file>"
    echo "`basename $0` d <file>: Deletes all descriptions for <file>"
    ;;
esac
